package com.tanhua.server.service;

import com.tanhua.model.domain.db.ErrorResult;
import com.tanhua.server.exceotion.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


@Service
public class UserFreezeService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //判断用户是否被冻结登录
    public void checkUserStatus(Long userId) {
        Object freezingRange = redisTemplate.opsForHash().get("freeze_" + userId, "freezingRange");
        if (ObjectUtils.isEmpty(freezingRange)){
            return;
        }
        Integer value = Integer.valueOf(String.valueOf(freezingRange));
        if(!ObjectUtils.isEmpty(value)) {
            if(value == 1) {
                throw new BusinessException(ErrorResult.builder().errMessage("您的账号被冻结！").build());
            }
        }
    }

    //判断用户是否被冻结发言
    public void checkUserSendStatus(Long userId) {
        Object freezingRange = redisTemplate.opsForHash().get("freeze_" + userId, "freezingRange");
        if (ObjectUtils.isEmpty(freezingRange)){
            return;
        }
        Integer value = Integer.valueOf(String.valueOf(freezingRange));
        if(!ObjectUtils.isEmpty(value)) {
            if(value == 2) {
                throw new BusinessException(ErrorResult.builder().errMessage("您的账号被冻结发言！").build());
            }
        }
    }

    //判断用户是否被冻结动态
    public void checkUserMovementStatus(Long userId) {
        Object freezingRange = redisTemplate.opsForHash().get("freeze_" + userId, "freezingRange");
        if (ObjectUtils.isEmpty(freezingRange)){
            return;
        }
        Integer value = Integer.valueOf(String.valueOf(freezingRange));
        if(!ObjectUtils.isEmpty(value)) {
            if(value == 3) {
                throw new BusinessException(ErrorResult.builder().errMessage("您的账号被冻结发布动态！").build());
            }
        }
    }
}
